
Type CalCoreSkeleton

	Field m_vectorCoreBone:CalCoreBone[]
	Field m_mapCoreBoneNames:StringMap
	Field m_vectorRootCoreBoneId:Int[]
	
	
	Method New()
	
		m_vectorCoreBone = New CalCoreBone[0]
		m_mapCoreBoneNames = New StringMap
		m_vectorRootCoreBoneId = New Int[0]
	
	End Method
	
	Method addCoreBone:Int(coreBone:CalCoreBone)
		' get next bone id
		Local boneId:Int = m_vectorCoreBone.length
		DebugLog "Bonesize: "+boneid
		m_vectorCoreBone = m_vectorCoreBone[..boneId+1] 'push(coreBone)
		m_vectorCoreBone[boneId] = coreBone
		' if necessary, add the core bone to the root bone list
		If coreBone.getParentId() = -1 Then
			Local bn:Int = m_vectorRootCoreBoneId.length
			m_vectorRootCoreBoneId = m_vectorRootCoreBoneId[..bn+1]
			m_vectorRootCoreBoneId[bn] = boneId
		EndIf
	
		' add a reference from the bone's name to its id
		mapCoreBoneName(boneId, coreBone.getName())
	
		Return boneId	
	
	End Method
	
	Method calculateState()
		For Local iRootCoreBoneId:Int = 0 To m_vectorRootCoreBoneId.length-1
			m_vectorCoreBone[m_vectorRootCoreBoneId[iRootCoreBoneId]].calculateState()
		Next		
	End Method


	Method getCoreBonebyID:CalCoreBone(coreBoneId:Int)
	
		If coreBoneId < 0 Or coreBoneId >= m_vectorCoreBone.length
			DebugLog "Invalid Bone ID: "+coreBoneId
			Return Null
		EndIf
	
		Return m_vectorCoreBone[coreBoneId]		
	
	End Method


	Method getCoreBone:CalCoreBone(coreBoneId:String)
		Local bid:Int = getCoreBoneId(coreBoneId)
		If bid < 0 Or bid >= m_vectorCoreBone.length
			DebugLog "Invalid Bone ID: "+coreBoneId
			Return Null
		EndIf
	
		Return m_vectorCoreBone[bid]		
	
	End Method
	
	Method getCoreBoneId:Int(name:String)
	
		If m_mapCoreBoneNames.Contains(name) = True Then
			Return Int(m_mapCoreBoneNames.Get(name))
		Else
			Return -1
		EndIf	
	End Method
	
	
	Method mapCoreBoneName:Byte(coreBoneId:Int, name:String)

		If coreBoneId < 0 Or coreBoneId >= m_vectorCoreBone.length Then
			Return False
		EndIf
		
		Return m_mapCoreBoneNames.Add(name,coreBoneId)
		
	
	End	Method
	
	
	Method getVectorRootCoreBoneId:Int[]()
		Return m_vectorRootCoreBoneId
	End Method
	
	Method getVectorCoreBone:CalCoreBone[]()
		Return m_vectorCoreBone
	End Method
	
	Method calculateBoundingBoxes(coreModel:CalCoreModel)
		For Local boneId:Int= 0 To m_vectorCoreBone.length-1
			'DebugLog('Calc Bone '+boneId);
			m_vectorCoreBone[boneId].calculateBoundingBox(coreModel)
		Next	
	End Method

	Method scale(factor:Float)
		For Local boneId:Int= 0 To m_vectorCoreBone.length-1
			'DebugLog('Calc Bone '+boneId);
			m_vectorCoreBone[iRootCoreBoneId].scale(factor)
		Next	
	End Method	
	

End Type

